#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <atomic>
#include <sys/stat.h>
#include <sys/time.h>
#include <boost/algorithm/string.hpp>

namespace ns_util
{
    class TimeUtil
    {
    public:
        static std::string GetTimeStamp()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec);
        }
        // 获得毫秒时间戳
        static std::string GetTimeMs()
        {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }
    };

    const std::string temp_path = "./temp/";
    class PathUtil
    {
    private:
        static std::string AddSuffix(const std::string &file_name, const std::string &suffix)
        {
            std::string pathname = temp_path;
            pathname += file_name;
            pathname += suffix;
            return pathname;
        }

    public:
        // 形成编译时所需要的3个临时文件路径名
        static std::string Src(const std::string &file_name)
        {
            return AddSuffix(file_name, ".cpp");
        }
        static std::string Exe(const std::string &file_name)
        {
            return AddSuffix(file_name, ".exe");
        }
        static std::string Compile_Error(const std::string &file_name)
        {
            return AddSuffix(file_name, ".compile_error");
        }
        // 形成运行时所需要的3个临时文件路径名
        static std::string Stdin(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string StdErr(const std::string &file_name)
        {
            return AddSuffix(file_name, ".stderr");
        }
    };

    class FileUtil
    {
    public:
        static bool IsFileExists(const std::string &path_name)
        {
            struct stat st;
            if (!stat(path_name.c_str(), &st))
                return true;
            return false;
        }
        static std::string UniqFileName() // 毫秒级时间戳 + 原子性递增计数器
        {
            static std::atomic_uint id(0);
            std::string timeStamp_ms = TimeUtil::GetTimeMs();
            std::string count = std::to_string(++id);
            return timeStamp_ms + "_" + count;
        }

        static bool WriteFile(const std::string &target, const std::string &content)
        {
            std::ofstream ofs(target);
            if (!ofs.is_open())
                return false;
            ofs.write(content.c_str(), content.size());
            ofs.close();
            return true;
        }

        static bool ReadFile(const std::string &target, std::string *content, bool keep = false)
        {
            std::ifstream ifs(target);
            if (!ifs.is_open())
                return false;
            std::string line;
            while (std::getline(ifs, line))
            {
                (*content) += line;
                (*content) += (keep ? "\n" : "");
            }
            ifs.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SplitString(const std::string &str, std::vector<std::string> *target, const std::string &sep)
        {
            boost::split(*target, str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}